<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div{
      width: 425px;
      height: 600px;
      border: 1px solid #000;
    }
    .left{
      float: left;
    }
    .left img{
      height: 400px;
      width: 400px;
    }
    .right{
      float: right;
    }
    .right img{
      height: 400px;
      width: 400px;
    }
  </style>
</head>
<body>
  <div class="left">
    <p><h2>源地址</h2></p>
    <img src="./轮播1.jpg" alt="" draggable="true">
  </div>
  <div class="right">
    <p><h2>目标地址</h2></p>
  </div>
  <script>
    let img = document.querySelector('img')
    let box1 = document.querySelector('.left')
    let box2 = document.querySelector('.right')

    img.addEventListener('dragstart',(e) =>{
      // 开始拖动图片时，存储图片地址
      e.dataTransfer.setData('text',img.src)
    }) 
    box2.addEventListener('dragenter',(e)=>{
      e.preventDefault();
      console.log('鼠标进入目标位置后触发');
    }) 
    box2.addEventListener('dragover',(e)=>{
      e.preventDefault();
      console.log('松开鼠标前会一直触发该事件');
    }) 
    box2.addEventListener('drop',(e)=>{
      e.preventDefault();
      // 鼠标松开后，触发该事件

      // 拿到已存储的数据
      let mysrc = e.dataTransfer.getData('text')
      // 创建新的img标签并赋予图片地址
      let myimg = document.createElement('img')
      myimg.src = mysrc
      // 添加标签到目标地址中
      box2.appendChild(myimg)
      // 清除存储的地址
      e.dataTransfer.clearData('text')

      // 删除源地址的图片
      // box1.removeChild(img)
    }) 
  </script>
</body>
</html>